﻿//https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/

class Solution {
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool vist[101][101];
    int ret;
    int n, m, k;

public:
    int wardrobeFinishing(int _m, int _n, int cnt)
    {
        m = _m;
        n = _n;
        k = cnt;

        dfs(0, 0);
        return ret;
    }

    void dfs(int i, int j)
    {
        vist[i][j] = true;
        ret++;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k];
            int y = j + dy[k];

            if (x >= 0 && x < m && y >= 0 && y < n && !vist[x][y] && check(x, y))
            {
                dfs(x, y);
            }
        }
    }

    bool check(int i, int j)
    {
        int temp = 0;
        while (i)
        {
            temp += i % 10;
            i /= 10;
        }
        while (j)
        {
            temp += j % 10;
            j /= 10;

        }

        return temp <= k ? true : false;
    }
};